package com.gxuwz.business.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.Student;
import com.gxuwz.util.Pager;

@Repository("studentDAO")
public class StudentDAO extends BaseDaoImpl<StudentDAO>{

	/**
	 * 分页查询(条件查询)
	 */
	public Pager<Student> getByPager(Student student, Integer page, Integer limit){
		String hql = "from Student where 1=1 and status = 0";
		//模糊查询
		if(null != student.getStuDesc() && !"".equals(student.getStuDesc())){
			String key = student.getStuDesc();
			hql += " and (stuId like '%"+key+"%' or stuName like '%"+key+"%' or stuSex like '%"+key+"%' "
					+ "or stuAge like '%"+key+"%' or stuGrade like '%"+key+"%' or stuCollege like '%"+key+"%' "
							+ "or stuMajor like '%"+key+"%' or careType like '%"+key+"%' or regType like '%"+key+"%')";
		}
		//时间段查询
		if(null != student.getOpenTime() && null != student.getEndTime()){
			hql += "and regDate between '"+student.getOpenTime()+"' and '"+student.getEndTime()+"' ";
		}
		return super.find(hql, null, null, (page-1)*limit, limit);
	}
	
	/**
	 * 添加挂号信息
	 */
	public boolean add(Student student){
		try{
			getHibernateTemplate().save(student);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 检测学号是否已存在
	 */
	public boolean checkStuId(Student student){
		return checkExist(Student.class, "stuId", student.getStuId());
	}
	
	/**
	 * 伪删除
	 */
	public boolean delete(Student student){
		boolean status = false;
		String hql = "update Student set status = 1 where regId = '"+student.getRegId()+"'";
		try{
			getHibernateTemplate().bulkUpdate(hql);
			status = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return status;   
	}
	
	/**
	 * 获取全部
	 */
	public List<Student> getAll(){
		String hql = "from Student where status = 0";
		try {
			return (List<Student>) getHibernateTemplate().find(hql);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据挂号编号查询
	 */
	public Student findById(Student student){
		String hql = "from Student where regId = '"+student.getRegId()+"'";
		try {
			return (Student) getHibernateTemplate().find(hql, null).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 只更新姓名、性别、年龄、年级、学院、班别、医保类型、挂号类型
	 */
	public boolean update(Student s){
		boolean isSucceed = false;
		String hql = "update Student set stuName=?, stuSex=?, stuAge=?, stuGrade=?, stuCollege=?, stuMajor=?"
				+ ",regType=?,careType=? where regId=?";
		try {
			int count = super.getHibernateTemplate().bulkUpdate(hql, s.getStuName(),s.getStuSex(),s.getStuAge(),s.getStuGrade()
					,s.getStuCollege(),s.getStuMajor(),s.getRegType(),s.getCareType(), s.getRegId());
//			System.out.println("更改了"+count+"条数据");
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSucceed;
	}
	
	/**
	 * 变更治疗状态
	 */
	public void updateState(String regId, Integer state){
		String hql = "update Student set state = ? where regId = ?";
		try {
			getHibernateTemplate().bulkUpdate(hql, state, regId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新全部
	 */
	public boolean updates(Student student){
		boolean isSucceed = false;
		try {
			getHibernateTemplate().update(student);
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSucceed;
	}
	
	/**
	 * 获取已完成治疗的
	 */
	public List<Student> getAllSucceed(){
		String hql = "from Student where status = 0 and state = 3";
		try {
			return (List<Student>) getHibernateTemplate().find(hql);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

}
